import sys, os
import pygame
import math
from pygame.draw import *

def deviation(points):
    length=len(points)
    avg=0.0
    for x in points:
        avg+=x
    avg/=length
    div=0
    for x in points:
        div+=(x-avg)**2
    div/=length-1.0
    div=math.sqrt(div)
    return div
    
# class Note(object):
#     def __init__(self,a,b):
#          super(Note, self).__init__()
#          self.locX = a
#          self.locY=b
#          self.speed=15.0
#          
#          
#     def update(self,num):
#         if self.locX>690 or self.locX<110:
#             self.speed*=-1.0
#        
#         self.locX+=self.speed
#         self.locY=num
#        
#     
#     def draw(self,screen):
#         pygame.draw.circle(screen,(0,0,0),(self.locX,self.locY),10)

class barlines (object):
    def __init__(self,a,b):
        super(barlines, self).__init__()
        self.segments = 40
        self.locX= a
        self.locY= b
        self.length= 1600
        self.bump =(0,0)
        self.points = [(self.locX,self.locY), (self.locX+self.length,self.locY)]
        self.dev=0
        self.shift=5
        self.WaveHeight=-200
        self.WavePosition= (-10000,self.locY)
        self.WaveSpeed=20.0
        self.WaveWidth=.75
        self._SpeedC = 20
        self.r = 0
        self.g = 0
        self.b = 0
    def copy(self,new_bar):
        if isinstance(new_bar,barlines):
            self.points = [x for x in new_bar.points]
            self.dev= new_bar.dev
            self.shift= new_bar.shift
            self.WavePosition= new_bar.WavePosition
            self.WaveSpeed=new_bar.WaveSpeed
            self.r = new_bar.r
            self.g = new_bar.g
            self.b = new_bar.b
        else:
            print "nope"

    def curveLoc(self,point):
        #uses a bell curve to determine the height at a given location
        temp=(self.WavePosition[0]-point)*self.WaveWidth

        val=(2.71828183**(-1.0*((temp)/100.0)**2.0/2.0)/(math.sqrt(2*3.14159265)))* self.WaveHeight+self.locY
       #  "hi-"+str(val)+" "+str(point)
        return val



    def thump (self):
        self.WavePosition=(self.locX+self.length+self.WaveHeight,self.locY)
        self.dev=self.curveLoc(700.0)
        #print (str(self.dev))

    def update(self, screen_rect,time):
        self.WaveSpeed = self._SpeedC*time
        if self.WavePosition[0]>-10000:
            self.WavePosition=(self.WavePosition[0]-self.WaveSpeed,self.locY)
#            self.note.update(self.curveLoc(self.note.locX))
        
        
    def draw(self, screen):
        #points=[(0,0),(24,15),(48,45),(72,45),(96,15),(120,0)]
        points=[]
        dist=self.length/self.segments
        for x in range(self.segments):
            points.append((dist*x+self.locX,self.curveLoc(float(dist*x+self.locX))))
        pygame.draw.lines (screen,(self.r,self.g,self.b),False,points,4)
    def set_color(self,r,g,b):
        self.r = r
        self.g = g
        self.b = b
#        self.note.draw(screen)


# class Game(object):
#     """main gameworld object"""
#     def __init__(self):
#         super(Game, self).__init__()
#         pygame.init()
#         self.screen = pygame.display.set_mode((1200, 600))
#         self.screen_rect = self.screen.get_rect()
#         self.clock = pygame.time.Clock()
#         self.barlines1 = barlines(100,100)
#         self.barlines2 = barlines(100,150)
#         self.barlines3 = barlines(100,200)
#         self.barlines4 = barlines(100,250)
#         self.barlines5 = barlines(100,300)
#         
#         
#         
#     def process_events(self):
#         """process all game events"""
#         for event in pygame.event.get():
#             if event.type == pygame.KEYDOWN:
#                 if event.key == pygame.K_ESCAPE:
#                     sys.exit(0)
#                 if event.key == pygame.K_w:
#                     self.barlines1.thump()
#                     self.barlines2.thump()
#                     self.barlines3.thump()
#                     self.barlines4.thump()
#                     self.barlines5.thump()
#                 
#                 
#     def update(self):
#         """updates the game"""
#         self.barlines1.update(self.screen_rect)
#         self.barlines2.update(self.screen_rect)
#         self.barlines3.update(self.screen_rect)
#         self.barlines4.update(self.screen_rect)
#         self.barlines5.update(self.screen_rect)
#         
# 
#         
#     def draw(self):
#         """draw the gameworld"""
#         self.screen.fill((255,255,255))
#         self.barlines1.draw(self.screen)
#         self.barlines2.draw(self.screen)
#         self.barlines3.draw(self.screen)
#         self.barlines4.draw(self.screen)
#         self.barlines5.draw(self.screen)
        #self.player.draw(self.screen)
        #self.score_surf=self.font.render("Score: %d"%self.ball.score,1,(255,255,255))
        #self.score_rect=self.score_surf.get_rect()
        #self.score_rect.top = 250
        #self.screen.blit(self.score_surf,self.score_rect)